package com.example.demo.excel;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.example.demo.entity.NewCar1;
import com.example.demo.entity.NewCar2;
import com.example.demo.entity.TwoCompany;
import com.example.demo.entity.TwoCompany2;
import com.example.demo.repository.TwoCompany2Repository;
import com.example.demo.repository.TwoCompanyRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.ws.Action;
import java.io.FileInputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/Excel2")
public class ExcelOutController2 {
    private DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd ");
    @Autowired
    private TwoCompanyRepository TwoCompanyRepository1Dao;
    @Autowired
    private TwoCompany2Repository TwoCompanyRepository2Dao;

    //二手车公司录入
    @RequestMapping(value = "/twocompanyIn")
    public void twocompanyIn(MultipartFile inFile) throws Exception {
        ImportParams params = new ImportParams();
        params.setTitleRows(3);//标题列站几行
        params.setHeadRows(2);//header列站几行
        List<TwoCompanyExcel> testExcel = ExcelImportUtil.importExcel(inFile.getInputStream(), TwoCompanyExcel.class, params);
		testExcel.forEach(System.out::println);
        for (TwoCompanyExcel newCar : testExcel) {
            TwoCompany2 newCar2 = new TwoCompany2();
            BeanUtils.copyProperties(newCar,newCar2,"name");
            TwoCompany newCar1 = new TwoCompany();
            newCar1.setName(newCar.getName());
            newCar2.setBeginDate(new Date());
            newCar2.setTwoCompany(newCar1);
            TwoCompanyRepository1Dao.save(newCar1);
            TwoCompanyRepository2Dao.save(newCar2);
        }
    }


}
